var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var controller = require(tools.rootPath + 'libs/controller.js');

module.exports = class extends controller {

    constructor() {
        super();
    }

    async index(req, res) {
        var page = req.query.page || 1;
        page = parseInt(page);

        var pagesize = 2;
        var offset = (page - 1) * pagesize;
        var total_page = await model.value("select count(*) from article");
        var lst = await model.select(`SELECT * from article order by itemid desc limit ${offset},${pagesize}`);
        res.render(req.TPL, { data: lst, url: config.url, total_page: total_page, page: page });
    }
    async add(req, res) {
        if (req.ispost) {
            var post = req.body;
            var title = post.title.trim();
            if (!title) {
                res.render('msg', { code: 0, msg: '标题不能为空', url: '' });
                return;
            }
            var content = post.content.trim();
            if (!content) {
                res.render('msg', { code: 0, msg: '内容不能为空', url: '' });
                return;
            }
            let insertId = await model.i("INSERT INTO article SET ?", post);
            if (insertId) {
                res.render('msg', { code: 1, msg: '操作成功', url: '/admin/article/index' });
            } else {
                res.render('msg', { code: 0, msg: '操作失败', url: '' });
            }
            return;
        }
        res.render(req.TPL, {});
    }
    async mod(req, res) {
        if (req.ispost) {
            var post = req.body;
            var itemid = parseInt(req.query.itemid);

            if (!itemid) {
                res.render('msg', { code: 0, msg: '数据不存在', url: '' });
                return;
            }

            var title = post.title.trim();
            if (!title) {
                res.render('msg', { code: 0, msg: '标题不能为空', url: '' });
                return;
            }
            var content = post.content.trim();
            if (!content) {
                res.render('msg', { code: 0, msg: '内容不能为空', url: '' });
                return;
            }
            let r = await model.u("update article SET ? where itemid=?", post, [itemid]);
            if (r) {
                res.render('msg', { code: 1, msg: '操作成功', url: '/admin/article/index' });
            } else {
                res.render('msg', { code: 0, msg: '操作失败', url: '' });
            }
            return;
        }

        var itemid = parseInt(req.query.itemid);
        if (!itemid) {
            res.render('msg', { code: 0, msg: '数据不存在', url: '' });
            return;
        }
        let data = await model.find("SELECT * from article where itemid=?", [itemid]);
        res.render(req.TPL, { data: data });
    }
    async del(req, res) {
        var itemid = parseInt(req.query.itemid);
        if (!itemid) {
            res.render('msg', { code: 0, msg: '数据不存在', url: '' });
            return;
        }
        let r = await model.delete("delete from article where itemid=?", [itemid]);
        return this.msg(r,'','/admin/article/index');
    }

};